package com.example.mobile2021606176mhq0626;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    OkHttpClient okHttpClient = new OkHttpClient();
    private TextView textView;
    private TextView tvMainVis;
    private TextView tvMainCondition;
    private TextView tvMainConditionId;
    private TextView tvMainHumidity;
    private TextView tvMainWindDir;
    private TextView tvMainTemp;
    private TextView tvMainWindLevel;
    private ImageView imageView;
    private ImageView imageView2;
    private ImageView imageView3;
    private TextView tvMainUpdatetime;
    private ImageView imageView4;
    private TextView tvMainPredictDate;
    private ImageView imageView5;
    private TextView tvMainWindDirNight;
    private TextView tvMainWindLevelNight;
    private TextView tvMainCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        {
                            RequestBody requestBody = new FormBody.Builder()
                                    .add("cityId", "917")
                                    .add("token", MojiDataManager.CONDITIONTOKEN)
                                    .build();
                            Request request = new Request.Builder()
                                    .addHeader("Authorization", "APPCODE " + MojiDataManager.APPCODE)
                                    .url(MojiDataManager.CONDITIONURL)
                                    .post(requestBody)
                                    .build();
                            Call call = okHttpClient.newCall(request);
                            call.enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    String result = response.body().string();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Gson gson = new Gson();
                                            Condition condition = gson.fromJson(result, Condition.class);
                                            tvMainVis.setText("能见度：\n" + condition.getData().getCondition().getVis()+"米");
                                            tvMainCondition.setText(condition.getData().getCondition().getCondition());
                                            tvMainHumidity.setText("湿度：\n" + condition.getData().getCondition().getHumidity() + "%");
                                            tvMainWindDir.setText(condition.getData().getCondition().getWindDir());
                                            tvMainWindLevel.setText(condition.getData().getCondition().getWindLevel() + "级");
                                            tvMainTemp.setText(condition.getData().getCondition().getTemp() + "°");
                                            Log.i("TAG", "run: " + result);
                                        }
                                    });

                                }
                            });
                            RequestBody requestBody2 = new FormBody.Builder()
                                    .add("cityId", "917")
                                    .add("token", MojiDataManager.BRIEFFORECAST6DAYSTOKEN)
                                    .build();
                            Request request2 = new Request.Builder()
                                    .addHeader("Authorization", "APPCODE " + MojiDataManager.APPCODE)
                                    .url(MojiDataManager.BRIEFFORECAST6DAYSURL)
                                    .post(requestBody2)
                                    .build();
                            Call call2 = okHttpClient.newCall(request2);
                            call2.enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    String result2 = response.body().string();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Gson gson = new Gson();
                                            SixDay sixDay = gson.fromJson(result2, SixDay.class);
                                            tvMainUpdatetime.setText("本地时间：\n" + sixDay.getData().getForecast().get(0).getUpdatetime());
                                            tvMainPredictDate.setText("日期：\n" + sixDay.getData().getForecast().get(0).getPredictDate());
                                            tvMainWindLevelNight.setText(sixDay.getData().getForecast().get(0).getWindLevelNight() + "级");
                                            tvMainWindDirNight.setText(sixDay.getData().getForecast().get(0).getWindDirNight());
                                            Log.i("TAG", "run: " + result2);

                                        }
                                    });

                                }
                            });

                        }
                    }
                }).start();
            }
        },0, 600000);
    }

    private void initView() {

        tvMainVis = (TextView) findViewById(R.id.tvMainVis);
        tvMainCondition = (TextView) findViewById(R.id.tvMainCondition);
        tvMainConditionId = (TextView) findViewById(R.id.tvMainHumidity);
        tvMainHumidity = (TextView) findViewById(R.id.tvMainHumidity);
        tvMainWindDir = (TextView) findViewById(R.id.tvMainWindDir);
        tvMainTemp = (TextView) findViewById(R.id.tvMainTemp);
        tvMainWindLevel = (TextView) findViewById(R.id.tvMainWindLevel);
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView2 = (ImageView) findViewById(R.id.imageView2);
        imageView3 = (ImageView) findViewById(R.id.imageView3);
        tvMainUpdatetime = (TextView) findViewById(R.id.tvMainUpdatetime);
        imageView4 = (ImageView) findViewById(R.id.imageView4);
        tvMainPredictDate = (TextView) findViewById(R.id.tvMainPredictDate);
        imageView5 = (ImageView) findViewById(R.id.imageView5);
        tvMainWindDirNight = (TextView) findViewById(R.id.tvMainWindDirNight);
        tvMainWindLevelNight = (TextView) findViewById(R.id.tvMainWindLevelNight);
        tvMainCity = (TextView) findViewById(R.id.tvMainCity);
    }
}